home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / matrix.lha / Matrix / perturb.C < prev    next >
C/C++ Source or Header  |  1992-03-26  |  2KB  |  84 lines

  1. #include <signal.h>
  2. #include <math.h>
  3. #include <bool.h>
  4. #include <MLCG.h>
  5. #include <Normal.h>
  6. #include <GetOpt.h>
  7. #include <double.Matrix.h>
  8. #define  matrix doubleMatrix
  9. #define  array  doubleArray
  10.  
  11. int
  12. main (int argc, char **argv)
  13.   {
  14.     int        verbose  = FALSE;        // verbose reporting off
  15.     long    seedval     = 43;            // random number seed value
  16.     double    sigma     = 0.0;            // noise variance
  17.     GetOpt    getopt (argc, argv, "n:s:v");
  18.  
  19.   int option;
  20.   while ((option = getopt()) != EOF)
  21.     switch (option)
  22.       {
  23.         case 'n':                // noise level
  24.       sigma        = atof(getopt.optarg);
  25.           break;
  26.         case 's':                // seed value
  27.       seedval    = atoi(getopt.optarg);
  28.           break;
  29.         case 'v':                // verbose reporting on
  30.       verbose    = TRUE;
  31.           break;
  32.         case '?':
  33.           cerr << "Unrecognized option!\n";
  34.       };
  35.  
  36.     if (verbose)
  37.       cerr    << " seedval = "    << seedval
  38.         << " sigma = "        << sigma << "\n";
  39.  
  40.     int        layers; cin >> layers;        // number of layers
  41.     matrix    b[layers];            // threshold biases
  42.     matrix    W[layers];            // connection weights
  43.  
  44.     int        outputs; cin >> outputs;    // number of outputs
  45.     int        inputs = outputs;        // number of  inputs
  46.     if (verbose)
  47.       cerr << "N(" << inputs;
  48.     int layer;
  49.     for (layer = 0; layer < layers; layer++)
  50.       {
  51.     int inputs = outputs;
  52.     cin >> outputs;
  53.     if (verbose)
  54.       cerr << ", " << outputs;
  55.  
  56.     MLCG    gen(13, 1023);
  57.     Normal    rnd (0.0, 1.0, &gen);
  58.         b[layer].resize(outputs);
  59.     W[layer].resize(outputs, inputs);
  60.     for (int output = 0; output < outputs; output++)
  61.       {
  62.       b[layer][0][output] = rnd()*sigma/(inputs+1);
  63.       for (int input = 0; input < inputs; input++)
  64.         W[layer][output][input] = rnd()*sigma/(inputs+1);
  65.       }
  66.       };
  67.     if (verbose)
  68.       cerr << ")\n";
  69.  
  70.     format("%12.5e", 6, " ");
  71.  
  72.     cout << layers << "\n";
  73.     cout << inputs << "\n";
  74.     for (layer = 0; layer < layers; layer++)
  75.       {
  76.     int outputs = b[layer].n();
  77.     cout << outputs << "\n";
  78.     for (int output = 0; output < outputs; output++)
  79.           cout << form("%12.5e\n", b[layer][0][output])
  80.            << W[layer].s(output);
  81.       };
  82.     cout.flush();
  83.   }
  84.